<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2007 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version

  $Source$
  $Revision$
  $Author$
  $Date$
**********************************************/
if (!defined('ADMIN_PAGES')) { exit; }
if (!can_admin('database')) { die('Access Denied'); }

$crlf = "\n";
$filename = $dbname.'_'.formatDateTime(time(), _DATESTRING3).'.sql';
$mode = (isset($_POST['mode']) && !$CLASS['member']->demo && Security::check_post()) ? $_POST['mode'] : '';
$type = strtoupper(substr($mode,0,-2));
if (isset($_POST['switchdb'])) $mode = '';
if (isset($_POST['tablelist']) && !isset($_POST['switchdb'])) {
	$tablelist = $_POST['tablelist'];
	$full = false;
} else {
	$tablelist = $db->list_tables();
	$full = true;
}

$pagetitle .= ' '._BC_DELIM.' '._DATABASE;

set_time_limit(0);

function show($mode, $tablelist, $query)
{
	global $db, $bgcolor2, $bgcolor3, $type, $dbname;
	require_once('header.php');
	GraphicAdmin('_AMENU0');
	OpenTable();
	if ($query === null) {
		echo 'Mode: <b>'.$mode.'</b> not available yet';
		return;
	}
	if (count($tablelist)) {
		$result = $db->sql_query($query);
		$numfields = $db->sql_numfields($result);
		echo '<span class="genmed"><strong>'._DATABASE.':</strong> '.$dbname.'</span><br /><br />Here are the results of your '.strtolower($type).'<br /><br />
		<table border="0" cellpadding="2"><tr bgcolor="'.$bgcolor2.'">';
		for ($j=0; $j<$numfields; $j++) {
			echo '<td><strong>'.$db->sql_fieldname($j, $result).'</strong></td>';
		}
		echo '</tr>';
		$bgcolor = $bgcolor3;
		while ($row = $db->sql_fetchrow($result)) {
			$bgcolor = ($bgcolor == '') ? ' bgcolor="'.$bgcolor3.'"' : '';
			echo '<tr'.$bgcolor.'>';
			for($j=0; $j<$numfields; $j++) {
				echo '<td>'.$row[$j].'</td>';
			}
			echo '</tr>';
		}
		echo '</table>';
	}
	CloseTable();
}

function run_sql_in_background($db_name, $type, $db, $query)
{
	$result = $db->sql_query($query);
	$numfields = $db->sql_numfields($result);
	$txt = 'Here are the results of your '.strtolower($type).' request on '.$db_name."\n\n";
	$txt .= str_pad($db->sql_fieldname(0, $result), 50);
	for ($j=1; $j<$numfields; $j++) { $txt .= str_pad($db->sql_fieldname($j, $result), 30); }
	$txt .= "\n".str_pad('', (40*$numfields), '=')."\n";
	while ($row = $db->sql_fetchrow($result))
	{
		$txt .= str_pad($row[0], 50);
		for ($j=1; $j<$numfields; $j++) { $txt .= str_pad($row[$j], 30); }
		$txt .= "\n";
	}
	file_put_contents(BASEDIR.'cache/sql_result.txt', $txt);
}

switch ($mode) {

	case 'BackupDB':
		if (empty($tablelist)) { cpg_error('No tables found'); }
		require_once(CORE_PATH.'classes/sqlctrl.php');
		SQLCtrl::backup($dbname, $tablelist, $filename, isset($_POST['dbstruct']), isset($_POST['dbdata']), isset($_POST['drop']), isset($_POST['gzip']), $full);
		break;

	case 'OptimizeDB':
		if (DB_TYPE == 'postgresql') {
			$db->query('VACUUM ANALYZE');
			$query = 'SELECT cl.relname as tablename, st.* FROM pg_class AS cl, pg_statistic AS st WHERE st.starelid=cl.relfilenode AND cl.relkind IN(\'r\') AND cl.relname NOT LIKE \'pg_%\' AND cl.relname NOT LIKE \'sql_%\' ORDER by cl.relname';
		} else {
			$query = "$type TABLE ".implode(", ", $tablelist);
		}
//		register_shutdown_function('run_sql_in_background', $dbname, $type, $db, $query);
		show($mode, $dbname, $tablelist, $query);
		break;

	case 'CheckDB':
		if (DB_TYPE == 'postgresql') {
			show($mode, $tablelist, null);
			break;
		} else {
		 	$query = "$type TABLE ".implode(", ", $tablelist).' EXTENDED';
		}
		show($mode, $tablelist, $query);
		break;
	case 'AnalyzeDB':
		if (DB_TYPE == 'postgresql') {
			$Module->sides = 0;
			$MAIN_CFG['global']['admingraphic'] =~ 2;
			if ($MAIN_CFG['global']['admingraphic'] == 0)  $MAIN_CFG['global']['admingraphic'] = 4;
			$db->query = 'ANALYZE';
			$query = 'SELECT tablename, attname, null_frac, avg_width, n_distinct, most_common_vals, most_common_freqs, correlation FROM pg_stats WHERE schemaname=\''.$schema.'\' ORDER BY tablename';
			//$query = 'SELECT * FROM pg_statistic';
		} else {
			$query = "$type TABLE ".implode(", ", $tablelist);
		}
		show($mode, $tablelist, $query);
		break;

	case 'RepairDB':
		if (DB_TYPE == 'postgresql') {
			$query = 'REINDEX '.$dbname;
		} else {
			$query = "$type TABLE ".implode(", ", $tablelist);
		}
		show($mode, $tablelist, $query);
		break;

	case 'StatusDB':
		$showblocks = 0;
		if (DB_TYPE == 'postgresql') {
			$schema = $db->sql_ufetchrowset('SELECT current_schema()');
			$query = 'SELECT relname, seq_scan, seq_tup_read, idx_scan, idx_tup_fetch, n_tup_upd, n_tup_del FROM pg_stat_user_tables WHERE schemaname = \''.$schema[0][0].'\' ORDER BY relname';
		} else {
			$query = "SHOW TABLE STATUS";
		}
		show($mode, $tablelist, $query);
		break;

	case 'RestoreDB':
		require_once('header.php');
		GraphicAdmin('_AMENU0');
		require_once(CORE_PATH.'classes/sqlctrl.php');
		if (!SQLCtrl::query_file($_FILES['sqlfile'], $error)) { cpg_error($error); }
		OpenTable();
		echo '<span class="genmed"><strong>'._DATABASE.': '.$dbname.'</strong></span><br /><br />Importation of <em>'.$_FILES['sqlfile']['name'].'</em> was successful';
		CloseTable();
		break;

	case 'Installer':
		SQLCtrl::installer($tablelist, false, isset($_POST['inst_structure']), isset($_POST['inst_data']), isset($_POST['inst_gzip']),
			array(
				'onduplicate' => isset($_POST['inst_onduplicate']) ? $_POST['inst_onduplicate'] : null,
				'datamode'    => isset($_POST['inst_datamode']) ? $_POST['inst_datamode'] : null
			)
		);
	break;

	case 'Synch':
		require_once('header.php');
		GraphicAdmin('_AMENU0');
		define('INSTALL', 1);
		require(BASEDIR."install/language/english.php");
		OpenTable();
		try {
			$db->begin();
			echo '<h3>'.$instlang['s3_sync_data'].':</h3>';
			if ($db->XML->syncSchemaFromFile(BASEDIR."install/sql/data/core.xml")) {
				foreach ($db->XML->queries as $query) {
					$db->query($query);
					echo '. ';flush();
				}
			} else {
				print_r($db->XML->errors);
				exit;
			}
			$db->commit();
			echo "<strong>{$instlang['s3_sync_done']}</strong>";
		} catch (Exception $e) {
			$db->rollback();
			echo $e.'<br /><br />'.$instlang['s1_fatalerror'];
		}
		Cache::clear();
		CloseTable();
	break;

	default:
		require_once('header.php');
		GraphicAdmin('_AMENU0');
		$cpgtpl->assign_vars(array(
			'U_ACTION' => URL::admin(),
			'S_DBTYPE' => DB_TYPE,
			'S_DATABASE' => $dbname,
			'ZLIB' => extension_loaded('zlib')
		));

		foreach ($tablelist as $table) {
			$cpgtpl->assign_block_vars('tables', array(
				'S_NAME' => $table
			));
		}
		$cpgtpl->set_handle('body', 'admin/database.html');
		$cpgtpl->display('body');

		break;
}
